package com.demo.freemarker.controller;

import com.demo.freemarker.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
public class HomeController {
    private final Logger logger = LoggerFactory.getLogger(HomeController.class);
    @RequestMapping("/")
    public String index() {
        logger.info("ok");
        return "index";
    }

    @RequestMapping("/test")
    public ModelAndView test(){
        ModelAndView mv = new ModelAndView("test");
        mv.addObject("name", "xiamo");
        mv.addObject("age", 12);
        Map<String, Object> map = new HashMap<>();
        map.put("password", "123456");
        mv.addObject("userList", prepareUserList());
        mv.addAllObjects(map);
        return mv;
    }

    public List<User> prepareUserList() {
        List<User> list = new ArrayList<>();
        for(int i = 0; i < 5; i++) {
            User item = new User();
            item.setId(i);
            item.setName("用户" + i);
            item.setBirthday(new Date());
            list.add(item);
        }
        return list;
    }
}
